<?php
namespace Common\Tool;

class Pager
{
    public $firstRow;
    public $listRows;
    public $parameter;
    public $nofollow;
    protected $totalPages;
    protected $totalRows;
    protected $nowPage;
    protected $coolPages;
    protected $rollPage;
    protected $config=array('header'=>'','prev'=>'<','next'=>'>','first'=>'<<','last'=>'>>');

    public function __construct($totalRows=0,$currentPage=1,$listRows=16,$attrs=null,$parameter='',$nofollow=true)
    {
        $this->nofollow = $nofollow ? 'rel="nofollow"' : '';
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->rollPage = 10;
        $this->listRows = !empty($listRows)?$listRows:24;
        $this->attrs = $attrs;
        $this->totalPages = ceil($this->totalRows/$this->listRows);
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($currentPage)?$currentPage:1;
        if($this->nowPage<1){$this->nowPage=1;}
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }

    // 格式 *-c-8v1_2_3-6.html，  8是分类cid，1,2,3为属性id，6是页码
    public function show()
    {
        if(0 == $this->totalRows) return '';
        $nowCoolPage = ceil($this->nowPage/$this->rollPage);
        $url = $_SERVER['REQUEST_URI'];
        preg_match('/\d+v(.*).html$/i', $url, $tmp);
        $page_str = '.html';
        if(isset($tmp[0])){
            $page_pos = strpos($tmp[0], '-');
            //url中有分页
            if($page_pos){
                $page_str = substr($tmp[0], $page_pos);
            }
            $url = str_replace($page_str, '', $url);
        }else{
            $url = str_replace($page_str, '', $url);
            $url .= 'v';
        }
        //
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        if ($upRow>0){
            if($upRow == 1){
                if(!is_array($this->attrs) || !count($this->attrs))
                {
                    $upPage="<a ".$this->nofollow." href='".substr($url,0,-1).".html' title='Previous'>".$this->config['prev']."</a>";
                }
                else
                {
                    $upPage="<a ".$this->nofollow." href='".$url.".html' title='Previous'>".$this->config['prev']."</a>";
                }
            }
            else
            {
                $upPage="<a ".$this->nofollow." href='".$url."-".$upRow.".html' title='Previous'>".$this->config['prev']."</a>";
            }
        }else{
            $upPage="";
        }

        if ($downRow <= $this->totalPages){
            $downPage="<a ".$this->nofollow." href='".$url."-".$downRow.".html' title='Next'>".$this->config['next']."</a>";
        }else{
            $downPage="";
        }
        // << < > >>
        if($nowCoolPage == 1){
            $theFirst = "";
        }else{
            $preRow =  $this->nowPage-$this->rollPage;
            $theFirst = "<a ".$this->nofollow." href='".$url."-1.html' title='first page' >".$this->config['first']."</a>";
        }
        if($nowCoolPage == $this->coolPages){
            $theEnd="";
        }else{
            $nextRow = $this->nowPage+$this->rollPage;
            $theEndRow = $this->totalPages;
            $theEnd = "<a ".$this->nofollow." href='".$url."-".$theEndRow.".html' title='last page' >".$this->config['last']."</a>";
        }
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$i<=$this->rollPage;$i++){
            $page=($nowCoolPage-1)*$this->rollPage+$i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    if($i==1)
                    {
                        if(!is_array($this->attrs) || !count($this->attrs))
                        {
                            if($this->nowPage >= $this->rollPage){
                                $linkPage .= "&nbsp;<a ".$this->nofollow." href='".$url."-".$page.".html'>&nbsp;".$page."&nbsp;</a>";
                            }else{
                                $linkPage .= "&nbsp;<a ".$this->nofollow." href='".substr($url, 0,-1).".html'>&nbsp;".$page."&nbsp;</a>";
                            }
                        }
                        else
                        {
                            $linkPage .= "&nbsp;<a ".$this->nofollow." href='".$url."-".$page.".html'>&nbsp;".$page."&nbsp;</a>";
                        }

                    }
                    else
                    {
                        $linkPage .= "&nbsp;<a ".$this->nofollow." href='".$url."-".$page.".html'>&nbsp;".$page."&nbsp;</a>";
                    }
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "&nbsp;<span class='currentpage'>".$page."</span>";
                }
            }
        }
        $pageStr = '<div class="pagenum">'.$theFirst.$upPage.$linkPage.$downPage.$theEnd.'</div>';
        return $pageStr;
    }

    // 格式： *-123.html， 123为页码
    public function show2()
    {
        if(0 == $this->totalRows) return '';
        $nowCoolPage = ceil($this->nowPage/$this->rollPage);
        $url = $_SERVER['REQUEST_URI'];
        preg_match('/-\d+.html$/i', $url, $tmp);
        $page_str = '.html';
        if(isset($tmp[0])){
            $url = str_replace($tmp[0], '', $url);
        }else{
            $url = str_replace($page_str, '', $url);
        }
        $url .= '-';
        //
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        if ($upRow>0){
            if($upRow == 1)
            {
                $furl = (substr($url, -1) == '-') ? substr($url, 0, -1) : $url;
                $upPage="<a ".$this->nofollow." href='".$furl.$page_str."' title='Previous'>".$this->config['prev']."</a>";
            }
            else
            {
                $upPage="<a ".$this->nofollow." href='".$url.$upRow.$page_str."' title='Previous'>".$this->config['prev']."</a>";
            }

        }else{
            $upPage="";
        }

        if ($downRow <= $this->totalPages){
            $downPage="<a ".$this->nofollow." href='".$url.$downRow.$page_str."' title='Next'>".$this->config['next']."</a>";
        }else{
            $downPage="";
        }
        // << < > >>
        if($nowCoolPage == 1){
            $theFirst = "";
            //$prePage = "";
        }else{
            $preRow =  $this->nowPage-$this->rollPage;
            //$prePage = "<a href='".$url."&".$p."=$preRow' >pre".$this->rollPage."page</a>";
            $theFirst = "<a ".$this->nofollow." href='".$url."1".$page_str."' title='first page' >".$this->config['first']."</a>";
        }
        if($nowCoolPage == $this->coolPages){
            //$nextPage = "";
            $theEnd="";
        }else{
            $nextRow = $this->nowPage+$this->rollPage;
            $theEndRow = $this->totalPages;
            //$nextPage = "<a href='".$url."&".$p."=$nextRow' >next".$this->rollPage."page</a>";
            $theEnd = "<a ".$this->nofollow." href='".$url.$theEndRow.$page_str."' title='last page' >".$this->config['last']."</a>";
        }
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$i<=$this->rollPage;$i++){
            $page=($nowCoolPage-1)*$this->rollPage+$i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    if($i==1)
                    {

                        $furl = (substr($url, -1) == '-') ? substr($url, 0, -1) : $url;
                        $linkPage .= "&nbsp;<a ".$this->nofollow." href='".$furl.$page_str."'>&nbsp;".$page."&nbsp;</a>";
                    }
                    else
                    {
                        $linkPage .= "&nbsp;<a ".$this->nofollow." href='".$url.$page.$page_str."'>&nbsp;".$page."&nbsp;</a>";
                    }
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "&nbsp;<span class='currentpage'>".$page."</span>";
                }
            }
        }
        $pageStr = '<div class="pagenum">'.$theFirst.$upPage.$linkPage.$downPage.$theEnd.'</div>';
        return $pageStr;
    }

    //  格式： /p123， 123是页码
    public function show3()
    {
        if(0 == $this->totalRows) return '';
        $nowCoolPage = ceil($this->nowPage/$this->rollPage);
        $url = $_SERVER['REQUEST_URI'];
        preg_match('/\/p(\d+)$/i', $url, $tmp);
        if(isset($tmp[0])){
            $url = str_replace($tmp[0], '', $url);
        }
        $furl = $url;
        $url .= '/p';
        //
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        if ($upRow>0){
            if($upRow == 1)
            {
                $upPage="<a ".$this->nofollow." href='".$furl."' title='Previous'>".$this->config['prev']."</a>";
            }
            else
            {
                $upPage="<a ".$this->nofollow." href='".$url.$upRow."' title='Previous'>".$this->config['prev']."</a>";
            }

        }else{
            $upPage="";
        }

        if ($downRow <= $this->totalPages){
            $downPage="<a ".$this->nofollow." href='".$url.$downRow."' title='Next'>".$this->config['next']."</a>";
        }else{
            $downPage="";
        }
        // << < > >>
        if($nowCoolPage == 1){
            $theFirst = "";
            //$prePage = "";
        }else{
            $preRow =  $this->nowPage-$this->rollPage;
            //$prePage = "<a href='".$url."&".$p."=$preRow' >pre".$this->rollPage."page</a>";
            $theFirst = "<a ".$this->nofollow." href='".$furl."' title='first page' >".$this->config['first']."</a>";
        }
        if($nowCoolPage == $this->coolPages){
            //$nextPage = "";
            $theEnd="";
        }else{
            $nextRow = $this->nowPage+$this->rollPage;
            $theEndRow = $this->totalPages;
            //$nextPage = "<a href='".$url."&".$p."=$nextRow' >next".$this->rollPage."page</a>";
            $theEnd = "<a ".$this->nofollow." href='".$url.$theEndRow."' title='last page' >".$this->config['last']."</a>";
        }
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$i<=$this->rollPage;$i++){
            $page=($nowCoolPage-1)*$this->rollPage+$i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    if($i==1)
                    {
                        $linkPage .= "&nbsp;<a ".$this->nofollow." href='".$furl."'>&nbsp;".$page."&nbsp;</a>";
                    }
                    else
                    {
                        $linkPage .= "&nbsp;<a ".$this->nofollow." href='".$url.$page."'>&nbsp;".$page."&nbsp;</a>";
                    }
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "&nbsp;<span class='currentpage'>".$page."</span>";
                }
            }
        }
        $pageStr = '<div class="pagenum">'.$theFirst.$upPage.$linkPage.$downPage.$theEnd.'</div>';
        return $pageStr;
    }
}